package com.itydl.a03.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author ydl
 * @version 快速索引控件
 */

public class FastIndexView extends View {
    private static final String TAG = "FastIndexView";

    private int mHeight;//控件的高度
    private int mWidth;//控件的宽度--25dp。同时也是单个控件所占的宽度
    private Paint mPaint;//画笔
    public static final String[] LETTERS = new String[]{
            "A", "B", "C", "D",
            "E", "F", "G", "H",
            "I", "J", "K", "L",
            "M", "N", "O", "P",
            "Q", "R", "S", "T",
            "U", "V", "W", "X",
            "Y", "Z" };
    private float mSingleHeight;//单个字符所占控件的高度
    private int mIndex;//手指按下或者移动时候计算出来的当前字母的索引
    private int lastIndex = -1;
    private OnFastIndexSelectedListener mIndexSelectedListener;

    /**
     * 调用此方法，设置选择索引栏监听器
     * @param indexSelectedListener
     */
    public void setOnIndexSelectedListener(OnFastIndexSelectedListener indexSelectedListener) {
        mIndexSelectedListener = indexSelectedListener;
    }

    public interface OnFastIndexSelectedListener{
        /**
         * 回调方法，同时把当前位置以及选中的字母传递出去
         * @param letter
         */
        void selected(int position,String letter);
    }

    public FastIndexView(Context context) {
        this(context,null);
    }

    public FastIndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //设置画笔基本属性
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿功能
        mPaint.setColor(Color.parseColor("#888888"));  //设置画笔颜色
        mPaint.setStyle(Paint.Style.FILL);//设置填充样式   Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
        mPaint.setTextSize(20);//设置绘制字体的大小
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //拿到控件的宽高
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        mSingleHeight = mHeight * 1.0f / LETTERS.length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < LETTERS.length; i++) {
            mPaint.setColor(i == mIndex ? Color.parseColor("#fa7829") : Color.parseColor("#888888"));
            String letter = LETTERS[i];

            //x = 控件宽度的一半 - 字母宽度的一半.mPaint.measureText(letter)可以测量当前文本的宽度
            float x = mWidth/2 - mPaint.measureText(letter)/2;
            //y = index*单个控件高度 + 控件高度的一半 + 字母高度的一半
            // 获取文本所占的矩形区域
            Rect bounds = new Rect();
            mPaint.getTextBounds(letter, 0, letter.length(), bounds);
//            float x = mWidth/2 - bounds.width()/2;//也可以通过这种方式获取字母的宽度
            float y = i * mSingleHeight + mSingleHeight/2 + bounds.height()/2f;
            canvas.drawText(letter,x,y,mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    float downY = event.getY();
                    mIndex = (int) (downY/mSingleHeight);
                    if(mIndex != lastIndex){
                        if(mIndex *mSingleHeight < downY && downY < (mIndex +1)*mSingleHeight && mIndexSelectedListener!=null){
                            String letter = LETTERS[mIndex];
                            Log.e(TAG,"ACTION_DOWN,当前字母："+letter);
                            lastIndex = mIndex;
                            mIndexSelectedListener.selected(mIndex,letter);
                        }
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    mIndex = (int) (event.getY() / mSingleHeight);
                    if (mIndex >= LETTERS.length) {
                        mIndex = LETTERS.length - 1;
                    }
                    if (mIndex != lastIndex) {
                        if (mIndex * mSingleHeight < event.getY() && event.getY() < (mIndex + 1) * mSingleHeight && mIndexSelectedListener != null) {
                            String letter = LETTERS[mIndex];
                            Log.e(TAG, "ACTION_MOVE,当前字母：" + letter);
                            lastIndex = mIndex;
                            mIndexSelectedListener.selected(mIndex, letter);
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    lastIndex = -1;
                    break;

                default:
                    break;
         }
        invalidate();
        return true;
    }
}
